package com.huaxin.hxcloud.work;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.huaxin.common.BaseActivity;
import com.huaxin.common.Constants;
import com.huaxin.common.Entity.RepositoryDTO;
import com.huaxin.common.tools.HxTransmitRequestHelper;
import com.huaxin.common.utils.SpacesItemDecoration;
import com.huaxin.hxcloud.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author lwjing
 * @date 2018/5/25
 * @Desc 力鼎知识库
 * Copyright HUAXIN DATA. All rights reserved.
 */
public class HxLDRepositoryActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar      mToolbar;
    @BindView(R.id.rl_plan_manager)
    RecyclerView mRl;
    private LDRepositoryAdapter ldRepositoryAdapter;
    List<RepositoryDTO.RepositoryType> repoDatas = new ArrayList<>();
    private View emptyView;

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.act_ld_repository;
    }

    @Override
    protected int getLoadFrameId() {
        return R.id.content;
    }

    @Override
    public void initView() {
        setSupportActionBar(mToolbar);
        mToolbar.setTitle("知识库");
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mRl.setLayoutManager(manager);
        ldRepositoryAdapter = new LDRepositoryAdapter(R.layout.item_plan_manager, repoDatas);
        mRl.setAdapter(ldRepositoryAdapter);
        mRl.addItemDecoration(new SpacesItemDecoration(30));

        emptyView = LayoutInflater.from(this).inflate(R.layout.item_empty_nodata_center, (ViewGroup) mRl.getParent(), false);

        ldRepositoryAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                itemClick(position);
            }
        });
    }

    private void itemClick(int position) {
        RepositoryDTO.RepositoryType repositoryType = repoDatas.get(position);
        Intent intent = new Intent();
        intent.setClass(this, HxLDRepositoryContentActivity.class);
        intent.putExtra(Constants.IK_REPO_ID, repositoryType.getId());
        intent.putExtra(Constants.TITLE_KEY, repositoryType.getText());
        startActivity(intent);
    }


    @Override
    protected void requestData() {
        apiFindTypeNode();
    }

    private void apiFindTypeNode() {
        updateLoadStatus(LOADING);
        Call<RepositoryDTO> call = HxTransmitRequestHelper.getRequestApi().getFindTypeNode();
        autoClose(call);
        call.enqueue(new Callback<RepositoryDTO>() {

            @Override
            public void onResponse(Call<RepositoryDTO> call, Response<RepositoryDTO> response) {
                RepositoryDTO contentDTO = response.body();
                if (contentDTO != null && contentDTO.isSuccessful()) {
                    updateLoadStatus(LOAD_SUCCESS);
                    repoDatas = contentDTO.getData();
                    if (repoDatas == null || repoDatas.size() == 0) {
                        ldRepositoryAdapter.setEmptyView(emptyView);
                    } else {
                        ldRepositoryAdapter.setNewData(repoDatas);
                    }

                } else {
                    updateLoadStatus(LOAD_ERROR);
                }
            }

            @Override
            public void onFailure(Call<RepositoryDTO> call, Throwable t) {
                updateLoadStatus(LOAD_FAILURE);
            }
        });
    }

    class LDRepositoryAdapter extends BaseQuickAdapter<RepositoryDTO.RepositoryType, BaseViewHolder> {


        public LDRepositoryAdapter(int layoutResId, @Nullable List<RepositoryDTO.RepositoryType> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, RepositoryDTO.RepositoryType repositoryType) {
            TextView tv_plan_colunm = helper.getView(R.id.tv_plan_colunm);
            tv_plan_colunm.setText(repositoryType.getText());
        }
    }
}
